package com.tarena.lbs.content.service.articleImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.ArticleRepository;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.po.ActionSearchPO;
import com.tarena.lbs.content.pojo.po.ArticleSearchPO;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository  articleRepository;

    @Override
    public PageResult<ArticleVO> pageList(ArticleQuery articleQuery) {
        log.info("查询文章列表{}",articleQuery);
        PageResult<ArticleVO> pageResult = new PageResult<>(articleQuery.getPageNo(),articleQuery.getPageSize());
        PageInfo<ArticleSearchPO> pageInfo =getPageArticle(articleQuery);
        pageResult.setTotal(pageInfo.getTotal());
        List<ArticleSearchPO> pos=pageInfo.getList();
        List<ArticleVO> vos=null;
        if (pos!=null&& pos.size()>0){
            vos=pos.stream().map(po->{
                ArticleVO vo=new ArticleVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        return pageResult;
    }
    private PageInfo<ArticleSearchPO> getPageArticle(ArticleQuery articleQuery) {
        PageHelper.startPage(articleQuery.getPageNo(),articleQuery.getPageSize());
        List<ArticleSearchPO> pos = articleRepository.list();
        return new PageInfo<>(pos);
    }
    @Override
    public void addArticle(ArticleContentParam param) {
        log.info("保存文章{}",param);
        ArticleSearchPO po=new ArticleSearchPO();
        BeanUtils.copyProperties(param,po);
        po.setId(1);
        po.setArticleTitle("11");
        po.setArticleCategoryId(1);
        po.setArticleLabel("11");
        po.setArticleDigest("11");
        po.setArticleCover("11");
        po.setArticleMainPic("11");
        po.setArticleDetail("11");
        po.setShopId(1);
        po.setActivityWord("11");
        po.setActivityId(1);
        po.setLatitude("11");
        po.setLongitude("11");
        po.setArticleStatus(1);
        po.setAccessCount(1);
        po.setLikeCount(1);
        po.setFavoriteCount(1);
        po.setSource(1);
        articleRepository.insert(po);
    }

    @Override
    public Set<String> articleLabels(ArticleQuery articleQuery) {
        log.info("查询文章标签{}",articleQuery);
        Set<String> articleLabels = articleRepository.articleLabels(articleQuery);
        return articleLabels;
    }

    @Override
    public ArticleVO articleDetail(String id) {
        log.info("查询文章详情{}",id);
        ArticleSearchPO po=articleRepository.articleDetail(id);
        ArticleVO vo=new ArticleVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    @Override
    public void articleBehavior(ArticleActionParam param) {
        log.info("用户行为记录{}",param);
        ActionSearchPO po=new ActionSearchPO();
        BeanUtils.copyProperties(param,po);
        po.setCreateTime(new Date());
        articleRepository.articleBehavior(po);
    }

    @Override
    public ArticleActionPageResultVO getBehaviorLists(ArticleQuery query) {
        log.info("查询用户行为记录{}",query);
        ActionSearchPO po=articleRepository.getBehaviorLists(query);
        ArticleActionPageResultVO resultVO=new ArticleActionPageResultVO();
        BeanUtils.copyProperties(po,resultVO);
        po.setArticleId(1);
        return resultVO;
    }


}
